import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QTreeView, QMessageBox, QFileSystemModel
from PyQt5.QtCore import Qt, QUrl
from PyQt5.QtGui import QDesktopServices

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("文件夹结构")
        self.setGeometry(100, 100, 400, 300)

        self.tree_view = QTreeView(self)
        self.tree_view.setRootIsDecorated(False)
        self.tree_view.setAlternatingRowColors(True)

        root_path = "D:/"  # 自定义根目录路径

        model = QFileSystemModel()
        model.setRootPath(root_path)
        self.tree_view.setModel(model)

        self.tree_view.setRootIndex(model.index(root_path))  # 设置根索引为自定义根目录

        self.tree_view.doubleClicked.connect(self.handle_double_click)  # 连接双击信号和处理函数

        self.setCentralWidget(self.tree_view)

    def handle_double_click(self, index):
        file_info = self.tree_view.model().fileInfo(index)
        if file_info.isFile():
            file_path = file_info.filePath()
            url = QUrl.fromLocalFile(file_path)
            QDesktopServices.openUrl(url)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())